#include "HelloWorldScene.h"
#include "GameRandom.h"
#include "TestLayer.h"
#include "Layer_GameBgAni.h"


USING_NS_CC;
USING_NS_CC_EXT;


Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    
    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    auto bk = LayerColor::create(Color4B(55, 88, 99, 255));
    //auto bk = Layer::create();
    bk->setContentSize(visibleSize);
    this->addChild(bk);
    
    // add a "close" icon to exit the progress. it's an autorelease object
    auto closeItem = MenuItemImage::create("CloseNormal.png", "CloseSelected.png",
                                           CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
    Size closeItemSize = closeItem->getContentSize();
    closeItem->setPosition(Vec2(visibleSize.width - closeItemSize.width/2, closeItemSize.height/2));
    
    // create menu, it's an autorelease object
    auto menu = Menu::create(closeItem, NULL);
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu, 1);
    
    std::string s = cocos2dVersion();
    s += "中文测试🌙➭➡";
    auto label = Label::createWithTTF(s, "fonts/test.ttf", 32);
    // position the label on the center of the screen
    label->setPosition(Vec2(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - label->getContentSize().height));
    
    // add the label as a child to this layer
    this->addChild(label, 1);
    
    // add "HelloWorld" splash screen"
    auto sprite = Sprite::create("HelloWorld.png");
    
    // position the sprite on the center of the screen
    sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
    // add the sprite as a child to this layer
    this->addChild(sprite, 2);
    auto moveTo = MoveTo::create(2, Vec2(visibleSize.width, visibleSize.height / 2));
    auto moveTo2 = MoveTo::create(2, Vec2(0, visibleSize.height / 2));
    sprite->runAction(RepeatForever::create(Sequence::create(moveTo, moveTo2, NULL)));
    
    //auto copy = Sprite::createWithTexture(sprite->getTexture());
    //bk->addChild(copy);
    
    auto colorBar = LayerColor::create(Color4B::GREEN);
    colorBar->setContentSize(Size(200, 50));
    colorBar->setPosition(Vec2(20, visibleSize.height - 50));
    this->addChild(colorBar);
    
    auto tint = TintTo::create(5, 255, 0, 0);
    auto tint2 = TintTo::create(5, 0, 255, 0);
    auto repeat = RepeatForever::create(Sequence::create(tint, tint2, NULL));
    colorBar->runAction(repeat);
    
    this->schedule(SEL_SCHEDULE(&HelloWorld::updateGame));
    
    auto btn = ControlButton::create("TEST", "Arial", 32);
    Size btnSize = btn->getContentSize();
    btn->addTargetWithActionForControlEvents(this, (Control::Handler)&HelloWorld::testButtonClick, Control::EventType::TOUCH_UP_INSIDE);
    //btn->setZOrder(100);
    //btn->setZoomOnTouchDown(false);
    btn->setTitleColorForState(Color3B::RED, Control::State::SELECTED);
    btn->setTitleColorForState(Color3B::GREEN, Control::State::NORMAL);
    btn->setTitleColorForState(Color3B::MAGENTA, Control::State::HIGH_LIGHTED);
    btn->setPosition(visibleSize.width / 2, btnSize.height / 2);
    btn->setPreferredSize(Size(btnSize.width * 2, btnSize.height));
    this->addChild(btn);
    
#if 0
    Size layerSize(50, 50);
    Size canvasSize(visibleSize.width * .8, visibleSize.height * .8);
    
    int cols = canvasSize.width / layerSize.width;
    int rows = canvasSize.height / layerSize.height;
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            CCLOG("Creating layer %d, %d", i, j);
            float x = visibleSize.width * .2 + j % cols * layerSize.width;
            float y = visibleSize.height * .2 + i % rows * layerSize.height;
            auto layer = Layer_GameBgAni::create();
            layer->setPosition(x, y);
            layer->setContentSize(layerSize);
            this->addChild(layer);
        }
    }
#endif
    return true;
}

static GameRandom rng;

void HelloWorld::updateGame(float dt)
{
    //CCLOG("%d", rng.getRandom(-1000, 1000));
}

void HelloWorld::testButtonClick(Ref* sender, Control::EventType eventType)
{
    Size mySize = this->getContentSize();
    //bk->setVisible(false);
    
#if 1
    
#endif
    
#if 0
    //auto login = GUIReader::getInstance()->widgetFromJsonFile("DemoLogin/DemoLogin.json");
    auto login = NodeReader::getInstance()->createNode("DemoLogin/DemoLogin.json");
    this->addChild(login);
    auto panel = login->getChildByName("Panel");
    auto img = panel->getChildByName("star_ImageView");
    CCLOG("%s", img->getDescription().c_str());
#endif
    
#if 0
    auto test = NodeReader::getInstance()->createNode("test/test_1.json");
    this->addChild(test);
    auto btn = test->getChildByName("Button");
    CCLOG("%s", btn->getDescription().c_str());
    auto panel = test->getChildByName("Panel");
    btn = panel->getChildByName("Button");
    CCLOG("%s", btn->getDescription().c_str());
#endif
    
#if 1
    auto layer = TestLayer::create();
    layer->setContentSize(Size(300, 250));
    layer->setPosition(350, 200);
    layer->initLayout();
    layer->setZOrder(100);
    this->addChild(layer);
    auto size = layer->getContentSize();
    //auto pos = layer->getPosition();
    
    /*
     auto layer2 = TestLayer::create();
     layer2->setContentSize(size);
     layer2->setPosition(pos.x + size.width, pos.y);
     layer2->initLayout();
     layer2->setZOrder(100);
     this->addChild(layer2);
     
     auto moveTo = MoveTo::create(3.5, pos);
     layer2->runAction(moveTo);*/
#endif
    
#if 0
    auto bar = Sprite::create("bar.png");
    Size barSize(mySize.width / 2, 50);
    Size barSpriteSize = bar->getContentSize();
    this->addChild(bar, 10);
    bar->setPosition(mySize.width / 2 / 2, mySize.height / 2);
    bar->setScale(barSize.width / barSpriteSize.width, barSize.height / barSpriteSize.height);
    
    auto bar2 = Sprite::create("bar.png");
    this->addChild(bar2, 10);
    bar2->setPosition(mySize.width / 4 * 3 + 1, mySize.height / 2);
    bar2->setScale(barSize.width / barSpriteSize.width, barSize.height / barSpriteSize.height);
#endif
    
#if 0
    auto r = cocostudio::GUIReader::getInstance();
    const char* json = "UITest/UITest.json";
    json = "test/test.json";
    auto widget = r->widgetFromJsonFile(json);
    this->addChild(widget);
#endif
    
#if 0
    auto t = Sprite::create("Icon-40.png");
    t->setVisible(false);
    this->addChild(t);
    auto sprite = Sprite::createWithTexture(t->getTexture(), Rect(0, 0, size.width, size.height));
    Texture2D::TexParams params = {GL_LINEAR,GL_LINEAR,GL_REPEAT,GL_REPEAT};
    sprite->getTexture()->setTexParameters(params);
    sprite->ignoreAnchorPointForPosition(true);
    this->addChild(sprite);
#endif
    
#if 0
    auto clippingNode = ClippingNode::create();
    clippingNode->setContentSize(Size(50, 50));
    
    auto stencil = LayerColor::create();
    //auto stencil = Layer::create();
    //auto stencil = DrawNode::create();
    //stencil->drawTriangle(Vec2(0, 0), Vec2(75, 150), Vec2(0, 150), Color4F::RED);
    //stencil->setOpacity(110);
    stencil->setContentSize(Size(150, 150));
    clippingNode->setStencil(stencil);
    clippingNode->setPosition(50, 150);
    //clippingNode->setAlphaThreshold(0);
    clippingNode->setContentSize(Size(0, 0));
    this->addChild(clippingNode);
    
    auto sprite = Sprite::create("Icon-100.png");
    clippingNode->addChild(sprite);
    
    auto moveTo = MoveTo::create(3, Vec2(150, 150));
    auto done = CallFunc::create(std::bind([sprite]{
        CCLOG("Action: 0x%lx", (intptr_t)sprite->getActionByTag(0));
    }));
    sprite->runAction(Sequence::create(moveTo, done, NULL));
#endif
}


void HelloWorld::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
	MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
    return;
#endif

    Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}
